using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Windows.Forms;
using IPAddress;
using RedGate.SQLDataGenerator.Engine.Generators;
using RedGate.SQLDataGenerator.Engine.Project;

namespace IPAddress
{
    public partial class IPv4AddressGeneratorUI : UserControl, IGeneratorSimpleUI
    {
        private SDGTable m_Table;
        private SDGField m_Field;
        private readonly IPv4AddressGenerator m_Generator;
        private readonly GeneratorParameters m_Parameters;
        private ModifiedGeneratorDataEventHandler m_Modified;


        public IPv4AddressGeneratorUI(IPv4AddressGenerator generator, GeneratorParameters parameters)
        {
            m_Generator = generator;
            m_Parameters = parameters;

            InitializeComponent();
        }
        
        public void ConfigureFieldAndTable(SDGTable table, SDGField field)
        {
            m_Table = table;
            m_Field = field;

            m_ClassA.Checked = m_Generator.IncludeRemoteClassAAddresses;
            m_ClassB.Checked = m_Generator.IncludeRemoteClassBAddresses;
            m_ClassC.Checked = m_Generator.IncludeRemoteClassCAddresses;
            m_Loopback.Checked = m_Generator.IncludeLoopbackAddresses;
            m_Multicast.Checked = m_Generator.IncludeMulticastAddresses;

            m_LocalIPAddresses.Checked = m_Generator.LocalAddresses;
            m_RemoteIPAddresses.Checked = !m_Generator.LocalAddresses;

        }

        public ModifiedGeneratorDataEventHandler Modified
        {
            get { return m_Modified; }
            set { m_Modified = value; }
        }

        private void SettingChanged()
        {
            m_Generator.IncludeRemoteClassAAddresses = m_ClassA.Checked;
            m_Generator.IncludeRemoteClassBAddresses = m_ClassB.Checked;
            m_Generator.IncludeRemoteClassCAddresses = m_ClassC.Checked;
            m_Generator.IncludeLoopbackAddresses = m_Loopback.Checked;
            m_Generator.IncludeMulticastAddresses = m_Multicast.Checked;
            m_Generator.LocalAddresses = m_LocalIPAddresses.Checked;

            if(m_Modified != null)
            {
                m_Modified(this, new ModifiedGeneratorDataEventArgs(
                                     m_Table, m_Field, false));
            }
        }

        private void CheckedChanged(object sender, EventArgs e)
        {
            SettingChanged();
        }
    }
}